package development.hdk.mindmapfornandroid;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.RelativeLayout;
import development.hdk.node.Node;

public class UserInterface implements View.OnTouchListener, GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener, ScaleGestureDetector.OnScaleGestureListener   
{
	private CommonResources commonResources;
	  private int fingerX = 0;
	  private int fingerY = 0;
	  private GestureDetector gestureScanner;
	  private boolean hasNodeMoved = false;
	  private ProcessorActivity main;
	  private boolean multitouch;
	  public int newX;
	  public int newY;
	  private Node root;
	  private ScaleGestureDetector scaleGestureScanner;
	  private View targetView;
	  public UserInterface(ProcessorActivity paramProcessorActivity, CommonResources paramCommonResources)
	  {
	    this.main = paramProcessorActivity;
	    this.commonResources = paramCommonResources;
	    this.gestureScanner = new GestureDetector(this);
	    this.scaleGestureScanner = new ScaleGestureDetector(paramProcessorActivity, this);
	  }
	@Override
	public boolean onScale(ScaleGestureDetector detector) {
		// TODO Auto-generated method stub
		//return false;
		this.commonResources.modifyZoom(detector.getScaleFactor(), (int)detector.getFocusX(), (int)detector.getFocusY());
	    this.root.updateAllLayout();
	    return true;
	}
	@Override
	public boolean onScaleBegin(ScaleGestureDetector detector) {
		// TODO Auto-generated method stub
		//return true;
		return true;
	}
	@Override
	public void onScaleEnd(ScaleGestureDetector detector) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		//return false;
		/*if ((this.targetView instanceof Node))
	    {
	      this.main.setFocus((Node)this.targetView);
	      this.main.showPanel((Node)this.targetView);
	    }*/
	    return true;
	}
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		/*if ((this.targetView instanceof Node))
	    {
	      this.main.setFocus((Node)this.targetView);
	      this.main.showCustomDialog(1);
	    }
	    while (true)
	    {
	      return;
	      if ((this.targetView instanceof RelativeLayout))
	      {
	        int[] arrayOfInt = new int[2];
	        this.main.relativeLayout.getLocationInWindow(arrayOfInt);
	        this.newX = Math.round(paramMotionEvent.getRawX() - arrayOfInt[0]);
	        this.newY = (Math.round(paramMotionEvent.getRawY()) - arrayOfInt[1]);
	        this.main.fromUI = true;
	        this.main.showCustomDialog(8);
	        continue;
	      }
	    }*/
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)//co fix 
	{
		
		// TODO Auto-generated method stub
		return false;
		/*int i = 0;
	    if (e2.getPointerCount() > 1)
	    {
	      return false;
	    }
	    if ((this.targetView instanceof Node))
	    {
	      this.hasNodeMoved = true;
	      ((Node)this.targetView).updateLayoutParams((int)e2.getRawX() - this.fingerX, (int)e2.getRawY() - this.fingerY);
	    }
	    while (true)
	    {
	      i = 1;
	      break;
	      if (!(this.targetView instanceof RelativeLayout))
	        continue;
	      if (this.multitouch)
	      {
	        this.multitouch = false;
	        continue;
	      }
	      this.commonResources.updateTopLeft(distanceX, distanceY);
	      this.root.updateAllLayout();
	    }*/
	}
	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		//return false;
		this.targetView = v;
	    if ((event.getAction() == 1) && (this.hasNodeMoved) && ((v instanceof Node)))
	    {
	      this.hasNodeMoved = false;
	      //this.main.saveNodeLocation((Node)paramView);
	    }
	    if ((event.getAction() == 0) && ((v instanceof Node)))
	    {
	      Node localNode = (Node)v;
	      this.fingerX = (int)(event.getRawX() - localNode.getNodeX());
	      this.fingerY = (int)(event.getRawY() - localNode.getNodeY());
	    }
	    onTouchEvent(event);
	    return true;
	}
	public boolean onTouchEvent(MotionEvent paramMotionEvent)
	  {
	    if (paramMotionEvent.getPointerCount() > 1)
	      this.multitouch = true;
	    this.gestureScanner.onTouchEvent(paramMotionEvent);
	    this.scaleGestureScanner.onTouchEvent(paramMotionEvent);
	    return true;
	  }
	public void setRoot(Node paramNode)
	  {
	    this.root = paramNode;
	  }
}
